﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Frame.WcfClient
{
    public class Process
    {
        public static WcfClient GetWcfClient(string strIp, string strHttpPort, string strTcpPort)
        {
            WSHttpBinding httpBinding = new WSHttpBinding();
            httpBinding.Security.Mode = SecurityMode.None;
            EndpointAddress httpAddress = new EndpointAddress("http://" + strIp + ":" + strHttpPort + "/Service/" + "Wcf/");
            httpBinding.MaxBufferPoolSize = 104857600;
            httpBinding.MaxReceivedMessageSize = 104857600;
            httpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            httpBinding.SendTimeout = new TimeSpan(0, 10, 0);

            NetTcpBinding tcpBinding = new NetTcpBinding();
            tcpBinding.Security.Mode = SecurityMode.None;
            EndpointAddress tcpAddress = new EndpointAddress("net.tcp://" + strIp + ":" + strTcpPort + "/Service/" + "Wcf/");
            tcpBinding.MaxBufferSize = 104857600;
            tcpBinding.MaxReceivedMessageSize = 104857600;
            tcpBinding.MaxBufferPoolSize = 104857600;
            httpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            httpBinding.SendTimeout = new TimeSpan(0, 10, 0);
            try
            {
                WcfClient client = new WcfClient(tcpBinding, tcpAddress);

                foreach (OperationDescription op in client.Endpoint.Contract.Operations)
                {
                    DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                                as DataContractSerializerOperationBehavior;
                    if (dataContractBehavior != null)
                    {
                        dataContractBehavior.MaxItemsInObjectGraph = 1000000;
                    }
                }
                return client;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
